package tmi.li.txwthr;

import java.io.StringReader;
import java.util.ArrayList;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;


public class Woeid {
	private String _appid = "WkarP8vV34Gw0TWqCQ3bGW_wkExS8yoVB9o2nelJJwOrUnMORtrVTNKSOj_rO0ojhAIX94UZwqu7ZwMKij7z.A--";
	private ArrayList<String> _cityname = new ArrayList<String>();
	private ArrayList<String> _county = new ArrayList<String>();
	private ArrayList<String> _county2 = new ArrayList<String>();
	private ArrayList<String> _country = new ArrayList<String>();
	public ArrayList<String> _woeid = new ArrayList<String>();

	
	
	public boolean getRss(String city) {
		city = city.replace(" ", "%20");
		try {
			HttpClient hc = new DefaultHttpClient();
			HttpGet test = new HttpGet("http://where.yahooapis.com/v1/places.q("+city+");count=5?appid="+_appid);
			HttpResponse rp = hc.execute(test);      	
			if(rp.getStatusLine().getStatusCode() == HttpStatus.SC_OK)
			{
				String str = EntityUtils.toString(rp.getEntity());
				return getInfo(str);
			}else {
				return false; 
			}

		}catch(Exception e) {
			return false; 
		}

	}

	public boolean getInfo(String str) {
		try {
			DocumentBuilder d = DocumentBuilderFactory.newInstance().newDocumentBuilder();
			StringReader reader = new StringReader( str );
			InputSource inputSource = new InputSource( reader );
			Document doc = null ;
			doc = d.parse( inputSource );
			reader.close();
			
			getWoeid(doc.getElementsByTagName("woeid"));
			getCountry(doc.getElementsByTagName("country"));
			getCounty(doc.getElementsByTagName("admin1"));
			getName(doc.getElementsByTagName("name"));
			getCounty2(doc.getElementsByTagName("admin2"));
			
			return true; 
		} catch (ParserConfigurationException e) {
			e.printStackTrace();
			return false;
		}catch (Exception e){
			e.printStackTrace();
			return false;
		}
	}
	
	
	private void getWoeid(NodeList nodes) {
		Node node;
		System.out.println(nodes.getLength());
		for(int i = 0; i < nodes.getLength(); i++) {
			node = nodes.item(i);
			if(node.getChildNodes().item(0).getNodeValue() != null)
				_woeid.add(node.getChildNodes().item(0).getNodeValue());
		}
	}
	
	
	private void getCountry(NodeList nodes) {
		Node node;
		for(int i = 0; i < nodes.getLength(); i++) {
			node = nodes.item(i);
			if(node.getAttributes().item(1).getNodeValue() != null)
				_country.add(node.getAttributes().item(1).getNodeValue());
		}
	}
	
	
	private void getCounty(NodeList nodes) {
		Node node;
		for(int i = 0; i < nodes.getLength(); i++) {
			node = nodes.item(i);
			if(node.getChildNodes().item(0).getNodeValue() != null) 
				_county.add(node.getChildNodes().item(0).getNodeValue());		
		}
	}
	
	private void getCounty2(NodeList nodes) {
		Node node;
		for(int i = 0; i < nodes.getLength(); i++) {
			node = nodes.item(i);
			if(node.hasChildNodes()) {
				if(node.getChildNodes().getLength() > 0) {
					if(node.getChildNodes().item(0).getNodeValue() != null) { 
						_county2.add(node.getChildNodes().item(0).getNodeValue());
						continue;
					}
				}
			}
			_county2.add("null");
		}
	}
	
	private void getName(NodeList nodes) {
		Node node;
		for(int i = 0; i < nodes.getLength(); i++) {
			node = nodes.item(i);
			if(node.getChildNodes().item(0).getNodeValue() != null) 
				_cityname.add(node.getChildNodes().item(0).getNodeValue());		
		}
	}
	
	
	public String getDesc(int i) {
		String str;
		str = _cityname.get(i);
		str = str + ((_county2.get(i) != "null") ? (", "+_county2.get(i)) : "");
		str = str + ", "+_county.get(i)+", "+_country.get(i);
		return str;
	}

}
